<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>My Project: Attribute.h 源文件</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">My Project
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.9.8 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','搜索');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function() { init_codefold(0); });
/* @license-end */
</script>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">载入中...</div>
<div class="SRStatus" id="Searching">搜索中...</div>
<div class="SRStatus" id="NoMatches">未找到</div>
</div>
</div>
</div>
</div>

<div class="header">
  <div class="headertitle"><div class="title">Attribute.h</div></div>
</div><!--header-->
<div class="contents">
<a href="Attribute_8h.html">浏览该文件的文档.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span> </div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span><span class="preprocessor">#pragma once</span></div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span> </div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno">   17</span><span class="preprocessor">#include &lt;trantor/utils/Logger.h&gt;</span></div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno">   18</span><span class="preprocessor">#include &lt;map&gt;</span></div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno">   19</span><span class="preprocessor">#include &lt;memory&gt;</span></div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno">   20</span><span class="preprocessor">#include &lt;any&gt;</span></div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno">   21</span> </div>
<div class="foldopen" id="foldopen00022" data-start="{" data-end="}">
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno"><a class="line" href="namespacedrogon.html">   22</a></span><span class="keyword">namespace </span><a class="code hl_namespace" href="namespacedrogon.html">drogon</a></div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno">   23</span>{</div>
<div class="foldopen" id="foldopen00028" data-start="{" data-end="};">
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno"><a class="line" href="classdrogon_1_1Attributes.html">   28</a></span><span class="keyword">class </span><a class="code hl_class" href="classdrogon_1_1Attributes.html">Attributes</a></div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno">   29</span>{</div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno">   30</span>  <span class="keyword">public</span>:</div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno">   39</span>    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div>
<div class="foldopen" id="foldopen00040" data-start="{" data-end="}">
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno"><a class="line" href="classdrogon_1_1Attributes.html#a6dcc0c4c9a7f553573930996f5f65199">   40</a></span>    <span class="keyword">const</span> T &amp;<a class="code hl_function" href="classdrogon_1_1Attributes.html#a6dcc0c4c9a7f553573930996f5f65199">get</a>(<span class="keyword">const</span> std::string &amp;key)<span class="keyword"> const</span></div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno">   41</span><span class="keyword">    </span>{</div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno">   42</span>        <span class="keyword">static</span> <span class="keyword">const</span> T nullVal = T();</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno">   43</span>        <span class="keyword">auto</span> it = attributesMap_.find(key);</div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno">   44</span>        <span class="keywordflow">if</span> (it != attributesMap_.end())</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno">   45</span>        {</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span>            <span class="keywordflow">if</span> (<span class="keyword">typeid</span>(T) == it-&gt;second.type())</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span>            {</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno">   48</span>                <span class="keywordflow">return</span> *(std::any_cast&lt;T&gt;(&amp;(it-&gt;second)));</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span>            }</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno">   50</span>            <span class="keywordflow">else</span></div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno">   51</span>            {</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno">   52</span>                LOG_ERROR &lt;&lt; <span class="stringliteral">&quot;Bad type&quot;</span>;</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno">   53</span>            }</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno">   54</span>        }</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno">   55</span>        <span class="keywordflow">return</span> nullVal;</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno">   56</span>    }</div>
</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span> </div>
<div class="foldopen" id="foldopen00061" data-start="{" data-end="}">
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"><a class="line" href="classdrogon_1_1Attributes.html#addd406636d707ec1e6c7502a36acf1b4">   61</a></span>    std::any &amp;<a class="code hl_function" href="classdrogon_1_1Attributes.html#addd406636d707ec1e6c7502a36acf1b4">operator[]</a>(<span class="keyword">const</span> std::string &amp;key)</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span>    {</div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno">   63</span>        <span class="keywordflow">return</span> attributesMap_[key];</div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span>    }</div>
</div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno">   65</span> </div>
<div class="foldopen" id="foldopen00073" data-start="{" data-end="}">
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"><a class="line" href="classdrogon_1_1Attributes.html#abb8720e9901aa00ee76039e5579d30fd">   73</a></span>    <span class="keywordtype">void</span> <a class="code hl_function" href="classdrogon_1_1Attributes.html#abb8720e9901aa00ee76039e5579d30fd">insert</a>(<span class="keyword">const</span> std::string &amp;key, <span class="keyword">const</span> std::any &amp;obj)</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno">   74</span>    {</div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno">   75</span>        attributesMap_[key] = obj;</div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno">   76</span>    }</div>
</div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno">   77</span> </div>
<div class="foldopen" id="foldopen00085" data-start="{" data-end="}">
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno"><a class="line" href="classdrogon_1_1Attributes.html#aac8fc549203b5793323e7c369faa32a4">   85</a></span>    <span class="keywordtype">void</span> <a class="code hl_function" href="classdrogon_1_1Attributes.html#aac8fc549203b5793323e7c369faa32a4">insert</a>(<span class="keyword">const</span> std::string &amp;key, std::any &amp;&amp;obj)</div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno">   86</span>    {</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno">   87</span>        attributesMap_[key] = std::move(obj);</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno">   88</span>    }</div>
</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno">   89</span> </div>
<div class="foldopen" id="foldopen00093" data-start="{" data-end="}">
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"><a class="line" href="classdrogon_1_1Attributes.html#af9e4dfe40a13d168c02e152e3b6c17f0">   93</a></span>    <span class="keywordtype">void</span> <a class="code hl_function" href="classdrogon_1_1Attributes.html#af9e4dfe40a13d168c02e152e3b6c17f0">erase</a>(<span class="keyword">const</span> std::string &amp;key)</div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno">   94</span>    {</div>
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno">   95</span>        attributesMap_.erase(key);</div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno">   96</span>    }</div>
</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno">   97</span> </div>
<div class="foldopen" id="foldopen00101" data-start="{" data-end="}">
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno"><a class="line" href="classdrogon_1_1Attributes.html#a927e9840c091578cf8caf8b60b1e6821">  101</a></span>    <span class="keywordtype">bool</span> <a class="code hl_function" href="classdrogon_1_1Attributes.html#a927e9840c091578cf8caf8b60b1e6821">find</a>(<span class="keyword">const</span> std::string &amp;key)</div>
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno">  102</span>    {</div>
<div class="line"><a id="l00103" name="l00103"></a><span class="lineno">  103</span>        <span class="keywordflow">if</span> (attributesMap_.find(key) == attributesMap_.end())</div>
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno">  104</span>        {</div>
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno">  105</span>            <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno">  106</span>        }</div>
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno">  107</span>        <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno">  108</span>    }</div>
</div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno">  109</span> </div>
<div class="foldopen" id="foldopen00113" data-start="{" data-end="}">
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno"><a class="line" href="classdrogon_1_1Attributes.html#a84f04a61bd7febcb39ee82be493a5d97">  113</a></span>    <span class="keywordtype">void</span> <a class="code hl_function" href="classdrogon_1_1Attributes.html#a84f04a61bd7febcb39ee82be493a5d97">clear</a>()</div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno">  114</span>    {</div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno">  115</span>        attributesMap_.clear();</div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno">  116</span>    }</div>
</div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno">  117</span> </div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno"><a class="line" href="classdrogon_1_1Attributes.html#ac26fe2f99092c7cd16a72633e5b68ced">  121</a></span>    <a class="code hl_function" href="classdrogon_1_1Attributes.html#ac26fe2f99092c7cd16a72633e5b68ced">Attributes</a>() = <span class="keywordflow">default</span>;</div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno">  122</span> </div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno">  123</span>  <span class="keyword">private</span>:</div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno">  124</span>    <span class="keyword">using </span>AttributesMap = std::map&lt;std::string, std::any&gt;;</div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno">  125</span>    AttributesMap attributesMap_;</div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno">  126</span>};</div>
</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno">  127</span> </div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"><a class="line" href="namespacedrogon.html#aa12d27459d310871d4c93167431514c9">  128</a></span><span class="keyword">using </span><a class="code hl_typedef" href="namespacedrogon.html#aa12d27459d310871d4c93167431514c9">AttributesPtr</a> = std::shared_ptr&lt;Attributes&gt;;</div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno">  129</span> </div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno">  130</span>}  <span class="comment">// namespace drogon</span></div>
</div>
<div class="ttc" id="aclassdrogon_1_1Attributes_html"><div class="ttname"><a href="classdrogon_1_1Attributes.html">drogon::Attributes</a></div><div class="ttdoc">This class represents the attributes stored in the HTTP request. One can add/get any type of data to/...</div><div class="ttdef"><b>Definition</b> Attribute.h:29</div></div>
<div class="ttc" id="aclassdrogon_1_1Attributes_html_a6dcc0c4c9a7f553573930996f5f65199"><div class="ttname"><a href="classdrogon_1_1Attributes.html#a6dcc0c4c9a7f553573930996f5f65199">drogon::Attributes::get</a></div><div class="ttdeci">const T &amp; get(const std::string &amp;key) const</div><div class="ttdoc">Get the data identified by the key parameter.</div><div class="ttdef"><b>Definition</b> Attribute.h:40</div></div>
<div class="ttc" id="aclassdrogon_1_1Attributes_html_a84f04a61bd7febcb39ee82be493a5d97"><div class="ttname"><a href="classdrogon_1_1Attributes.html#a84f04a61bd7febcb39ee82be493a5d97">drogon::Attributes::clear</a></div><div class="ttdeci">void clear()</div><div class="ttdoc">Clear all attributes.</div><div class="ttdef"><b>Definition</b> Attribute.h:113</div></div>
<div class="ttc" id="aclassdrogon_1_1Attributes_html_a927e9840c091578cf8caf8b60b1e6821"><div class="ttname"><a href="classdrogon_1_1Attributes.html#a927e9840c091578cf8caf8b60b1e6821">drogon::Attributes::find</a></div><div class="ttdeci">bool find(const std::string &amp;key)</div><div class="ttdoc">Return true if the data identified by the key exists.</div><div class="ttdef"><b>Definition</b> Attribute.h:101</div></div>
<div class="ttc" id="aclassdrogon_1_1Attributes_html_aac8fc549203b5793323e7c369faa32a4"><div class="ttname"><a href="classdrogon_1_1Attributes.html#aac8fc549203b5793323e7c369faa32a4">drogon::Attributes::insert</a></div><div class="ttdeci">void insert(const std::string &amp;key, std::any &amp;&amp;obj)</div><div class="ttdoc">Insert a key-value pair</div><div class="ttdef"><b>Definition</b> Attribute.h:85</div></div>
<div class="ttc" id="aclassdrogon_1_1Attributes_html_abb8720e9901aa00ee76039e5579d30fd"><div class="ttname"><a href="classdrogon_1_1Attributes.html#abb8720e9901aa00ee76039e5579d30fd">drogon::Attributes::insert</a></div><div class="ttdeci">void insert(const std::string &amp;key, const std::any &amp;obj)</div><div class="ttdoc">Insert a key-value pair</div><div class="ttdef"><b>Definition</b> Attribute.h:73</div></div>
<div class="ttc" id="aclassdrogon_1_1Attributes_html_ac26fe2f99092c7cd16a72633e5b68ced"><div class="ttname"><a href="classdrogon_1_1Attributes.html#ac26fe2f99092c7cd16a72633e5b68ced">drogon::Attributes::Attributes</a></div><div class="ttdeci">Attributes()=default</div><div class="ttdoc">Constructor, usually called by the framework</div></div>
<div class="ttc" id="aclassdrogon_1_1Attributes_html_addd406636d707ec1e6c7502a36acf1b4"><div class="ttname"><a href="classdrogon_1_1Attributes.html#addd406636d707ec1e6c7502a36acf1b4">drogon::Attributes::operator[]</a></div><div class="ttdeci">std::any &amp; operator[](const std::string &amp;key)</div><div class="ttdoc">Get the 'any' object identified by the given key</div><div class="ttdef"><b>Definition</b> Attribute.h:61</div></div>
<div class="ttc" id="aclassdrogon_1_1Attributes_html_af9e4dfe40a13d168c02e152e3b6c17f0"><div class="ttname"><a href="classdrogon_1_1Attributes.html#af9e4dfe40a13d168c02e152e3b6c17f0">drogon::Attributes::erase</a></div><div class="ttdeci">void erase(const std::string &amp;key)</div><div class="ttdoc">Erase the data identified by the given key.</div><div class="ttdef"><b>Definition</b> Attribute.h:93</div></div>
<div class="ttc" id="anamespacedrogon_html"><div class="ttname"><a href="namespacedrogon.html">drogon</a></div><div class="ttdoc">Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...</div><div class="ttdef"><b>Definition</b> Attribute.h:23</div></div>
<div class="ttc" id="anamespacedrogon_html_aa12d27459d310871d4c93167431514c9"><div class="ttname"><a href="namespacedrogon.html#aa12d27459d310871d4c93167431514c9">drogon::AttributesPtr</a></div><div class="ttdeci">std::shared_ptr&lt; Attributes &gt; AttributesPtr</div><div class="ttdef"><b>Definition</b> Attribute.h:128</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
制作者&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.8
</small></address>
</body>
</html>
